<?php
namespace Admin\Controller;
use Common\Controller\CoreController;
class UserController extends CoreController {

	public function index(){
        $UserModel = M('User');
        $userList = $UserModel->order('id desc')->page($page.',20')->select();
        $this->assign('list',$userList);
        
        $count = $UserModel->count();
        $Page = new \Think\Page($count,20);
        $show = $Page->show();
        $this->assign('page',$show);

        $this->assign('addText','添加用户');
        $this->assign('addNew',U('addUser'));
        $this->assign('edit',U('editUser'));
        $this->assign('del',U('delUser'));
        $this->display();
    }

    public function addUser(){
        $arr=array('a','b','c','d','e');
    	if(IS_POST){
    		$data = array(
    			'username' => I('post.username'),
    			'nickname' => I('post.nickname'),
    			'password' => MD5(I('post.password')),
    			'status' => I('post.status',0,'intval'),
                'psc'=>$arr[array_rand($arr)],
    			);
            $userInfo = M('User')->where("username='".I('post.username')."'")->find();
            if(!$userInfo){
                $userId = M('User')->add($data);
                if($userId){
                    $data = array();
                    foreach (I('post.roles') as $role) {
                        $data[] = array('role_id'=>$role,'user_id'=>$userId);
                    }
                    M('RoleUser')->addAll($data);
                    header('Location:'.U('index'));
                }else{
                   $this->error('操作失败'); 
                }
            }else{
                $this->error('用户已存在'); 
            }
    		
    	}else{
    		
    		$RoleModel = M('Role');
	        $roleList = $RoleModel->select();
	        $this->assign('roleList',$roleList);

	        $this->assign('action',U('addUser'));
	        $this->assign('cancel',U('index'));

	        //加载视图类型静态文件
            $this->setStaticFiles('AdvancedForm');
	        $this->display();
    	}
        
    }

    public function editUser(){
    	if(IS_POST){
    		$id = I('post.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }
    		$data = array(
    			'nickname' => I('post.nickname'),
    			'status' => I('post.status',0,'intval'),
    			);
            $password = I('post.password');
    		if(!empty($password)){
    			$data['password'] = MD5(I('post.password'));
    		}

    		$status = M('User')->where("id=".$id)->save($data);

			$data = array();
			foreach (I('post.roles') as $role) {
				$data[] = array('role_id'=>$role,'user_id'=>$id);
			}

			M('RoleUser')->where('user_id='.$id)->delete();
			M('RoleUser')->addAll($data);
			header('Location:'.U('index'));
    	}else{
    		$id = I('get.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }

            $userInfo = M('User')->where('id='.$id)->find();

            $RoleModel = M('Role');
	        $roleList = $RoleModel->select();
	        $this->assign('roleList',$roleList);

    		$RoleUserModel = M('RoleUser');
	        $roleUserList = $RoleUserModel->where('user_id='.$id)->select();
	        foreach ($roleUserList as $userId) {
	        	$roleUserIds[] = $userId['role_id'];
	        }
	        
	        $this->assign('id',$id);
	        $this->assign('userInfo',$userInfo);
	        $this->assign('roleUserIds',$roleUserIds);
	        $this->assign('action',U('editUser'));
	        $this->assign('cancel',U('index'));

	        //加载视图类型静态文件
            $this->setStaticFiles('AdvancedForm');
	        $this->display();
    	}
        
    }

    public function autoUser(){
        if(IS_AJAX){
            $user_name = I('post.name');
            if(!$user_name){
                $this->ajaxReturn(array());
            }
            $user = M('User')->field('id,username')->where("username LIKE '".$user_name."%'")->select();

            $this->ajaxReturn($user); 
        }
    }

    public function delUser(){
        $id = I('get.id',0,'intval');
        if($id<1){
            $this->error('错误的ID');
        }
        M('User')->where("id=".$id)->delete();
        header('Location:'.U('index'));
    }
}